<?php
declare (strict_types = 1);

namespace app\common\model;

use think\Model;

/**
 * @mixin think\Model
 */
class DingdingModel extends Model
{
    protected $name = 'messages';
    //
    function dingding_up($Dingmsg) { 
        
        $url = 'https://oapi.dingtalk.com/robot/send?access_token=xxxxx';
        $time = time() *1000;
        $SyTime = date('m-d-Y H:i:s');  //操作时间
        $secret = 'xxxxx';
        $sign = hash_hmac('sha256', $time . "\n" . $secret,$secret,true);
        $sign = base64_encode($sign);
        $sign = urlencode($sign);
        $msg = [
            'msgtype' => 'text',
            'text'    => [
                'content' => '系统通知：'.$Dingmsg.'，操作时间'.$SyTime.'',
            ],
        ];
        $url = "{$url}&timestamp={$time}&sign={$sign}";
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($msg));
        curl_setopt($curl, CURLOPT_HEADER, 0);
        curl_setopt($curl, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        $res = curl_exec($curl);
        curl_close($curl);
        // var_dump($res);  //不返回参数
    }

}
